// Copyright 2023 Huawei Cloud Computing Technology Co., Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef SERVICE_RRCONFIG_H
#define SERVICE_RRCONFIG_H

#include <string>
#include <map>

namespace rr {

const std::string DEFAULT_PRIVATE_IP = "0.0.0.0";
const uint16_t DEFAULT_MASTER_PORT = 5050;
const uint16_t DEFAULT_HEALTH_CHECK_PORT = 8080;
const std::string DEFAULT_HEALTH_CHECK_URL = "/health";
const uint32_t DEFAULT_HEARTBEAT_TIMEOUT = 30000000; // 单位微妙

const std::string DEFAULT_REDIS_ADDRESS = "127.0.0.1";
const uint16_t DEFAULT_REDIS_PORT = 6379;
const uint16_t DEFAULT_REDIS_TIMEOUT = 1000;
const std::string DEFAULT_REDIS_PASSWORD = "redis";

    class MasterConfig {
    public:

        class MASTER {
        public:
            std::string privateIp;
            uint16_t port;
            uint16_t healthCheckPort;
            std::string healthCheckUrl;
            uint32_t heartbeatTimeout;

            MASTER() {
                privateIp = DEFAULT_PRIVATE_IP;
                port = DEFAULT_MASTER_PORT;
                healthCheckPort = DEFAULT_HEALTH_CHECK_PORT;
                healthCheckUrl = DEFAULT_HEALTH_CHECK_URL;
                heartbeatTimeout = DEFAULT_HEARTBEAT_TIMEOUT;
            }

        } iMaster;

        class REDIS {
        public:
            std::string address;
            uint16_t port;
            uint32_t timeout;
            std::string password;

            REDIS() {
                address = DEFAULT_REDIS_ADDRESS;
                port = DEFAULT_REDIS_PORT;
                timeout = DEFAULT_REDIS_TIMEOUT;
                password = DEFAULT_REDIS_PASSWORD;
            }

        } iRedis;
    };

    extern MasterConfig gMasterConfig;

    class RrConfig {
    public:
        RrConfig() {
        }

        ~RrConfig() {
        }

        bool ReadConfig(const std::string &filename);

        std::string ReadString(const char *section, const char *item, const char *default_value);

        int ReadInt(const char *section, const char *item, const int &default_value);

        float ReadFloat(const char *section, const char *item, const float &default_value);

    private:
        bool IsSpace(char c);

        bool IsCommentChar(char c);

        void Trim(std::string &str);

        bool AnalyseLine(const std::string &line, std::string &section, std::string &key, std::string &value);

    private:
        std::map<std::string, std::map<std::string, std::string>> settings_;
    };
}

#endif //SERVICE_RRCONFIG_H
